package com.qf.dao;

import com.qf.pojo.BookInfo;
import com.qf.pojo.BookType;
import com.qf.utils.JpaUtils;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.Date;

public class BookInfoDao {
    public static void main(String[] args) {
        BookInfoDao dao=new BookInfoDao();
        dao.addBook();
        //dao.findByBookId();
    }

    public void addBook(){
        EntityManager session = JpaUtils.getSession();
        EntityTransaction transaction = session.getTransaction();
        transaction.begin();
        BookInfo pojo=new BookInfo();
        pojo.setBookName("修罗道");
        pojo.setBookAuthor("步非烟著");
        pojo.setBookPrice(99.8);
        pojo.setBookDate(new Date().getTime());
        pojo.setBookDesc("此书记载了唐传奇的故事......");
        pojo.setBookPublish("樽主出版社");
        BookTypeDao typeDao=new BookTypeDao();
        BookType id=typeDao.findById(1L);
        pojo.setBookType(id);
        session.persist(pojo);
        transaction.commit();
        session.close();
    }

    //多对一查询
    public void findByBookId(){
        EntityManager session = JpaUtils.getSession();
        BookInfo bookInfo = session.find(BookInfo.class, 1L);
        System.out.println("图书信息："+"《"+bookInfo.getBookName()+"》"+"\t"+bookInfo.getBookDesc()+"\t"+bookInfo.getBookAuthor());
        BookType bookType = bookInfo.getBookType();
        System.out.println("图书分类："+bookType.getTypeName()+"\t"+bookType.getTypeDesc());
        session.close();
    }
}
